<?php
namespace App\Services\ThirdPayments\Recharge;

use App\Lib\PaymentMap;
use App\Lib\ThirdPartyMap;
use App\Models\RechargeOrder;
use App\Services\ThirdPayments\Contracts\RechargeBaseAbstract;

class ESDPayment extends RechargeBaseAbstract
{
    protected $gw_pay = 'http://rui.amy0807.com/pay/payment';
    protected $gw_query = 'http://rui.amy0807.com/pay/payquery';

    public function callback(array $data)
    {
        // TODO: Implement callback() method.
    }

    public function veryCallbackSign(array $data)
    {
        // TODO: Implement veryCallbackSign() method.
    }

    public function getOrderNoFromCallback(array $data)
    {
        // TODO: Implement getOrderNoFromCallback() method.
    }

    /**
     * @return mixed
     */
    public function getIdentify(): string
    {
        // TODO: Implement getIdentify() method.
        return ThirdPartyMap::ESD;
    }

    /**
     *  needs child to achieve
     */
    public function initPaymentMap(): array
    {
        // TODO: Implement initPaymentMap() method.
        return [
            PaymentMap::WX => 'HFWXPay',
            PaymentMap::ALI_WAP => 'HFAliPayH5',
            PaymentMap::WX_WAP => 'YYKWXWAP',
            PaymentMap::QQ => 'HFQQPay',
            PaymentMap::QQ_WAP => 'YHXFQQWAP'
        ];
    }

    public function query(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement query() method.
    }

    public function bankHref(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement bankHref() method.
    }

    public function scanCode(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement scanCode() method.
        $this->initParameters($rechargeOrder);
        $res = curlHttp($this->getGwPay(), 'post', $this->getParameters());
        dd($res, $this->getGwPay(), $this->getParameters());

    }

    public function paySign(): string
    {
        // TODO: Implement paySign() method.
        $params = $this->getParameters();
        ksort($params);
        $signStr = '';
        foreach ($params as $k => $param) {
            $signStr .= $k . '=' . $param . '&';
        }
        $signStr = substr($signStr, 0, -1);
        $sign = $signStr . '&' . $this->getMchKey();
        return strtoupper(md5($sign));
    }

    public function querySign(): string
    {
        // TODO: Implement querySign() method.
    }

    public function showSuccess(): string
    {
        // TODO: Implement showSuccess() method.
    }

    public function initParameters(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement initParameters() method.
        $mchData = $rechargeOrder->order_data;
        $this->setParameter('version', 'v1.0');
        $this->setParameter('service', $this->getPaymentMap($mchData['recharge_type']));
        $this->setParameter('signtype', 'md5');
        $this->setParameter('charset', 'UTF-8');
        $this->setParameter('merchantid', $this->getMchId());
        $this->setParameter('shoporderId', $rechargeOrder->plat_no);
        $this->setParameter('totalamount', $rechargeOrder->order_amt);
        $this->setParameter('productname', $mchData['body']);
        $this->setParameter('goods_tag', json_encode([
            'uId' => $rechargeOrder->uid,
            'orderId' => $rechargeOrder->id
        ]));
        $this->setParameter('notify_url', $this->getCallbackUrl());
        $this->setParameter('callback_url', $this->getReturnUrl());
        $this->setParameter('mch_create_ip', request()->getClientIp());
        $this->setParameter('nonce_str', createNonceStr());
        $this->setParameter('sign', $this->paySign());
    }
}
